package com.example.unique.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.unique.databinding.FragmentHomeBinding
import com.example.unique.database.DatabaseJob

class HomeFragment : Fragment() {

    private lateinit var club: String
    private var _binding: FragmentHomeBinding? = null
    private val binding get() = _binding!!
    private lateinit var homeViewModel: HomeViewModel
    private lateinit var jobDatabase: DatabaseJob

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 初始化数据库实例
        jobDatabase = DatabaseJob.getInstance(requireContext())
        // 初始化 ViewModel
        homeViewModel = HomeViewModel(jobDatabase)
        // 从 Activity 获取社团信息
        club = activity?.intent?.getStringExtra("club").orEmpty()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        // 初始化 ViewBinding
        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // 初始化 RecyclerView
        binding.recyclerView.layoutManager = LinearLayoutManager(context)

    }

    override fun onDestroyView() {
        super.onDestroyView()
        // 清理资源
        _binding = null
    }
}
